<template>
	<view class="safe-area-inset-bottom">
		<view class="swip_header">
			<view class="slot-box">
				<view style="width: 100%;">
					<uni-search-bar v-model="queryParams.name" type="text" @confirm="handleQuery" @input="handleQuery"
						placeholder="请输入合同类型名称" />
				</view>
			</view>
			<!-- <view class="swiplist">
				<view >合同水印设置</view>
				<button size="mini" class="add_btn" @click="handleAdd"><i class="iconfont" style="color: #00a660;">&#xeaf3;</i>新增</span></button></view> -->
		</view>

		<!-- 列表 --> 
		<view v-if="waterMarkList != ''" class="listSearchTop cardList">
				<uni-card :is-shadow="false" v-for="(item,index) in waterMarkList" :key="index">
					<view class="flex_between">
					<p class="grid-hidden" style="width: 70%;">水印名称：{{ item.name }}</p>
					<switch color="#00a660" style="transform:scale(0.7)" :checked="item.status == 1 ?true:false"
						@change="switchChange(item)" v-if="item.isSystem == 1" disabled />
					<span v-else @click="switchChange(item)">
						<switch color="#00a660" style="transform:scale(0.7)" :checked="item.status == 1 ?true:false" disabled
						 />
					</span>
					</view>
					<p class="grid-integral">合同类型：
						<span v-if="!!item.contractTypeName">{{ item.contractTypeName }}</span>
						<span v-else>全部类型</span>
					</p>
					<view slot="actions" class="card-actions">
						<view class="grid-integral" v-if="item.updateTime != null">水印更新时间：{{item.updateTime}}</view>
						<view class="grid-integral" v-else>水印更新时间：--</view>
					  <view class="card-actions-item" @click="handleEdit(item)">
					    <text class="card-actions-item-text themeColor">编辑</text>
					  </view>
					  <view class="card-actions-item" @click="handleDelete(item,index)" v-if="item.isSystem == 0">
					    <text class="card-actions-item-text errorColor">删除</text>
					  </view>
					</view>	
			</uni-card>
			<!-- 加载更多 -->
			<uni-load-more color="#00a660" :status="loadStatus" />
		</view>
		<!-- 暂无数据 -->
		<luanqingEmpty :show="true" text="没有更多数据啦" v-else />
	</view>
</template>

<script>
	import {
		delWaterMark,
		listWaterMark,
		updateWaterMarkStatus
	} from "@/api/ec/console/busiConfig/waterMark";
	import luanqingEmpty from '@/components/luanqing-empty/luanqing-empty';
	export default {
		components: {luanqingEmpty},
		data() {
			return {
				total:0 ,//总条数
				loadStatus:'loading', //是否加载中 //加载样式：more-加载前样式，loading-加载中样式，nomore-没有数据样式
				waterMarkList: [], // 合同类型列表 
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					name: null,
			        isSystem: null,
					status: [],
				},
			}
		},
		onLoad() {
			this.getList()
			uni.$on('refresh',data=>{
				this.getList(true)
			})
		},
		//上拉触底函数
		onReachBottom() { 
			if (this.loadStatus != 'noMore' && this.total > this.waterMarkList.length) {
			  this.loadStatus = 'loading';
			  // 延迟500毫秒显示loading效果
			  setTimeout(() => {
			    // 执行查询的方法
			    this.getList(false);
			  }, 500)
			} else {
			  // 停止加载
			  this.loadStatus = 'noMore'
			}            
		},
		methods: {
			/** 搜索 */
			handleQuery() {
				this.getList(true);
			},
			/** 水印列表 */
			getList(reload) {
			  if (reload) {
			    this.queryParams.pageNum = 1;
			    this.loadStatus = 'more'
          this.$modal.loading();
			  }
			  listWaterMark(this.queryParams).then(response => {
			    if (reload) {
			      uni.hideLoading()
			    }
			    this.total = response.total;
			    if (response.total > 0) {
			      const dataRows = response.rows;
			      this.waterMarkList = reload ? dataRows : this.waterMarkList.concat(dataRows);
			    } else {
			      this.waterMarkList = [];
			    }
			    // 判断数据是否加载完全
			    if (response.total <= this.waterMarkList.length) {
			      this.loadStatus = 'noMore'
			    }
			    // 页数+1
			    this.queryParams.pageNum++
			  }).catch(() => {
			    if (reload) {
			      uni.hideLoading()
			    }
			  });
			},
			/** 水印状态 */
			switchChange(item) {
				let text = item.status == 1 ? "停用" : "启用";
				var that = this
				uni.showModal({
					title: '系统提示',
					confirmColor: '#00a660',
					content: '确认 " ' + text + ' " 该水印吗？',
					success: res=> {
						if (res.confirm) {
							let statusEx = item.status == 1 ? 0 : 1;
							const data = {
								id: item.id,
								status: statusEx
							}
							updateWaterMarkStatus(data).then(response => {
								that.getList(true)
                this.$modal.msg(text + '水印成功')
							})
						} else if (res.cancel) {}
					}
				});
			},
			/** 水印编辑 */
			handleEdit(item, index) {
				this.$tab.navigateTo('./watermarkSet?id='+item.id)	
			},
			/** 删除 */ 
			handleDelete(item,index){
				let that = this
				uni.showModal({
					title:'系统提示',
					content:'是否删除' + item.name + '？',
					confirmColor:'#00a660',
					success:function(res){
						if(res.confirm){
							delWaterMark(item.id);
							that.getList(true);
							that.$modal.msgSuccess("删除成功");
						}else{
						}
					}
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	::v-deep .uni-forms-item__label {
		width: 150rpx !important;
	}

	::v-deep .uni-popup .uni-popup__wrapper {
		width: 90% !important;
		padding: 0;
	}
</style>
